﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using Runda.EShop.Common.Utility;

namespace Runda.EShop.Web.Order
{
    public partial class OrderComplaintAdd : BasePage
    {
        public Guid OrderID
        {
            get
            {
                if (Request.QueryString["OrderID"] != null)
                {
                    return new Guid(Request.QueryString["OrderID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnCancel.Attributes.Add("onclick", "parent.$.ligerDialog.hide();return false;");
            if (!IsPostBack)
            { 
                OrderBll bll = new OrderBll();
                BizOrderEntity entity = bll.Get<BizOrderEntity>(OrderID);
                if (entity != null)
                {
                    ltlOrderCode.Text = entity.OrderCode;
                }

                EnumDescAttribute[] enums = EnumDescAttribute.GetFieldTexts(typeof(EnumHelper.OrderComplaintType));
                foreach (EnumDescAttribute enun in enums)
                {
                    ddlType.Items.Add(new ListItem(enun.EnumDisplayText, enun.EnumDisplayText));
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ltlOrderCode.Text.Trim()))
            {
                return;
            }
            BizOrderComplaintEntity entity = new BizOrderComplaintEntity();
            entity.CustomerID = base.User.Identity.LoginUser.UserID;
            entity.OrderID = OrderID;
            entity.OrderCode = ltlOrderCode.Text;
            entity.Type = ddlType.SelectedValue;
            entity.Status = EnumDescAttribute.GetFieldText(EnumHelper.OrderComplaintStatus.Pending);
            entity.Content = txtContent.Text;
            entity.CreatedAt = DateTime.Now;
            entity.Deleted = false;

            OrderBll bll = new OrderBll();
            bll.Insert(entity);

            ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('提交成功.');parent.$.ligerDialog.hide();", true);
        }
    }
}